/*
 * Copyright (c) 2000, 2002, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * -Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * -Redistribution in binary form must reproduct the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of Oracle nor the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
 * DAMAGES OR LIABILITIES  SUFFERED BY LICENSEE AS A RESULT OF  OR
 * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR
 * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
 * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
 * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
 * THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that Software is not designed, licensed or
 * intended for use in the design, construction, operation or
 * maintenance of any nuclear facility.
 */
package jbrowser.security;

/**
 * &lt;p&gt; This class implements the &lt;code&gt;Principal&lt;/code&gt;
 * interface and represents a Sample user.
 *
 * &lt;p&gt; Principals such as this &lt;code&gt;Principal&lt;/code&gt; may be
 * associated with a particular &lt;code&gt;Subject&lt;/code&gt; to augment that
 * &lt;code&gt;Subject&lt;/code&gt; with an additional identity. Refer to the
 * &lt;code&gt;Subject&lt;/code&gt; class for more information on how to achieve
 * this. Authorization decisions can then be based upon the Principals
 * associated with a &lt;code&gt;Subject&lt;/code&gt;.
 *
 * @see java.security.Principal
 * @see javax.security.auth.Subject
 */
public class Principal implements java.security.Principal, java.io.Serializable {

    /**
     * @serial
     */
    private String name;

    /**
     * Create a Principal with a Sample username.
     *
     * &lt;p&gt;
     *
     * @param name the Sample username for this user.
     *
     * @exception NullPointerException if the &lt;code&gt;name&lt;/code&gt; is
     * &lt;code&gt;null&lt;/code&gt;.
     */
    public Principal(String name) {
        if (name == null) {
            throw new NullPointerException("illegal null input");
        }

        this.name = name;
    }

    /**
     * Return the Sample username for this &lt;code&gt;Principal&lt;/code&gt;.
     *
     * &lt;p&gt;
     *
     * @return the Sample username for this &lt;code&gt;Principal&lt;/code&gt;
     */
    public String getName() {
        return name;
    }

    /**
     * Return a string representation of this
     * &lt;code&gt;Principal&lt;/code&gt;.
     *
     * &lt;p&gt;
     *
     * @return a string representation of this
     * &lt;code&gt;Principal&lt;/code&gt;.
     */
    public String toString() {
        return ("SamplePrincipal:  " + name);
    }

    /**
     * Compares the specified Object with this
     * &lt;code&gt;Principal&lt;/code&gt; for equality. Returns true if the
     * given object is also a &lt;code&gt;Principal&lt;/code&gt; and the two
     * SamplePrincipals have the same username.
     *
     * &lt;p&gt;
     *
     * @param o Object to be compared for equality with this
     * &lt;code&gt;Principal&lt;/code&gt;.
     *
     * @return true if the specified Object is equal equal to this
     * &lt;code&gt;Principal&lt;/code&gt;.
     */
    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }

        if (this == o) {
            return true;
        }

        if (!(o instanceof Principal)) {
            return false;
        }
        Principal that = (Principal) o;

        if (this.getName().equals(that.getName())) {
            return true;
        }
        return false;
    }

    /**
     * Return a hash code for this &lt;code&gt;Principal&lt;/code&gt;.
     *
     * &lt;p&gt;
     *
     * @return a hash code for this &lt;code&gt;Principal&lt;/code&gt;.
     */
    public int hashCode() {
        return name.hashCode();
    }
}
